﻿#region 文件信息

// FileName: SoCafeShop/Sohobloo.SoAuth.Service/FunctionService.cs
// Author: Sohobloo
// CreateTime: 2013/06/29 19:32
// UpdateTime: 2013/06/30 3:41

#endregion

#region 引用

using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using Sohobloo.SoAuth.Data.Repositories;
using Sohobloo.SoAuth.Entity;
using Sohobloo.SoMcCoffee.Public.OperationResult;

#endregion

namespace Sohobloo.SoAuth.Service.Impl
{
    [Export(typeof (IFunctionService))]
    internal class FunctionService : ServiceBase, IFunctionService
    {
        #region 属性
        [Import]
        IFunctionRepository FunctionRepository { get; set; }

        [Import]
        IFunctionParamRepository FunctionParamRepository { get; set; }

        [Import]
        IPermissionRepository PermissionRepository { get; set; }

        #endregion 属性

        #region 方法

        public OperationResult CreateFunction(SoAuthFunction function)
        {
            //检查功能名称重复
            if (FunctionRepository.Entities.Any(f => f.FunctionName == function.FunctionName))
            {
                return new OperationResult(OperationResultType.Conflict, "功能名称重复");
            }

            function.MenuLevel = function.ParentFunction != null ? function.ParentFunction.MenuLevel + 1 : 1;

            FunctionRepository.Insert(function, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult DeleteFunction(Guid functionId)
        {
            PermissionRepository.Delete(p => p.PermissionFunction.Id == functionId, false);

            var function = FunctionRepository.GetByKey(functionId);
            if(function.ChildrenFunctions.Any())
            {
                function.ChildrenFunctions.ToList().ForEach(
                    f =>
                    {
                        f.ParentFunction = null;
                        FunctionRepository.Update(f);
                    });
            }

            FunctionRepository.Delete(f => f.Id == functionId, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.NoChange);
        }

        public OperationResult UpdateFunction(SoAuthFunction function)
        {
            if (FunctionRepository.Entities.Any(f => f.Id != function.Id && f.FunctionName == function.FunctionName))
            {
                return new OperationResult(OperationResultType.Conflict, "功能名称重复");
            }

            function.MenuLevel = function.ParentFunction != null ? function.ParentFunction.MenuLevel + 1 : 1;

            FunctionParamRepository.Delete(fp => fp.ParamFunction.Id == function.Id, false);
            FunctionRepository.Update(function, false);

            return UnitOfWork.Commit() > 0
                       ? new OperationResult(OperationResultType.Success)
                       : new OperationResult(OperationResultType.Conflict);
        }

        public ICollection<SoAuthFunction> GetFunctionList()
        {
            return FunctionRepository.Entities.OrderBy(f => f.MenuLevel).ThenBy(f => f.MenuOrder).ToList();
        }

        public SoAuthFunction GetFunction(Guid functionId)
        {
            return FunctionRepository.GetByKey(functionId);
        }

        #endregion 方法
    }
}